var mbox = function () {
    //变量作用域为函数内部，外部无法访问
    var url = transferUrl("/main/loadSysMenu");

    var loadMenu = function (pid, callback) {
        $.post(url, {"pid": pid}, function (reuslt) {
            if (typeof callback == "function") {
                callback(reuslt);
            } else {
                return;
            }
        })
    };
    var tabsList = [];

    return {
        initMenu: function () {
            loadMenu(0, function (result) {
                if (!result)
                    return;
                $("#sys_menu").empty();
                for (var k in result) {
                    var name = result[k].name;
                    var id = result[k].id;
                    var menu = $("<li><a href='javascript:mbox.initSecondMenu(" + id + ");'>" + name + "</a></li>");
                    menu.click(function () {
                        $("li", $("#sys_menu")).removeClass("active").attr("class", "");
                        $(this).closest("li").addClass("active");
                    });

                    $("#sys_menu").append(menu);

                    if (k == 0) {
                        menu.addClass("active");
                        menu.click();
                        mbox.initSecondMenu(id);
                    }
                }
            });
        },
        initSecondMenu: function (pid) {
            loadMenu(pid, function (result) {
                if (typeof result != "object" || result==null){
                    window.location.reload();
                }
                $("#sys_secmenu").empty();
                for (var k in result) {
                    var name = result[k].name;
                    var id = result[k].id;
                    var url = result[k].url;
                    var menu = $("<li><a href='javascript:void();' data-name='" + name + "' data-id='" + id + "' data-url='" + url + "'>" + name + "</a></li>");
                    menu.click(function () {
                        $("li", $("#sys_secmenu")).removeClass("active");
                        $(this).addClass("active");
                        var name = $("a", $(this)).data("name");
                        var id = $("a", $(this)).data("id");
                        var url = $("a", $(this)).data("url");
                        if(url==null || url == ''){
                            url = 'system/blank'
                        }
                        mbox.newTab(name, url, id);
                    });

                    $("#sys_secmenu").append(menu);
                }
            });
        },
        initTopPage: function () {
            // mbox.addTab("首页", "/main/top.html", 0);
            mbox.newTab("首页", "/main/topPage", 0);
        },
        newTab: function (name, url, id) {
            url = transferUrl(url);
            if (typeof id == "undefined" || id == null) {
                id = name;
            }
            for (var tid in tabsList) {
                if (tabsList[tid] == id) {
                    mbox.chooseTab(id);
                    return;
                }
            }
            var tab = $("<li style='min-width:90px;text-align: center;'><a data-toggle='tab' href='#tabPane"+id+"' id='tab" + id + "'>" + name + "</a><button type='button' class='close' style='float:right;position:absolute;top:5px;right:5px;font-size: 16px;' onclick='mbox.removeTab(" + id + ")'>×</button></li>");
            var ifr = $("<div id='tabPane"+id+"' class='tab-pane fade in active full-height'>" +
                "<iframe src='" + url + "' id='mainContent" + id + "' name='mainContent"+ id +"' frameborder='0' scrolling='auto" +
                "' style='padding-bottom: 60px;text-align: center;' class='full-div'></iframe>" +
                "</div>");
            tab.click(function () {
                $("a",$(this))[0].click();
            });
            $("#sys_tabs").append(tab);
            $("#sys_iframes").append(ifr);
            tabsList.push(id);
            tab.click();
        },
        chooseTab: function (id) {
            $("#tab"+id)[0].click();
        },
        removeTab: function (id) {
            if (id == 0) {
                return;
            }
            $("#tab" + id).closest("li").remove();
            $("#tabPane" + id).remove();
            tabsList.length = tabsList.length - 1 > 0 ? tabsList.length - 1 : 1;
            mbox.chooseTab(0);
        }
    }
}();

function loginOut() {
    _confirm("系统提示", "确认退出系统？", {
        callback: function (v) {
            if (v) {
                $.post(transferUrl("/system/logout"), null, function (result) {
                    if (result.status == "500") {
                        sendToLogin();
                    }
                });
            }
        }
    });
}